﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>

   <% 
       List<string> hideFilters=new List<string>();

       if (ViewData["HideFilters"] != null)
       {
           hideFilters = ViewData["HideFilters"].ToString().Split(',').ToList();
       }
       
        Type type = Model.GetType();
        if (type.IsGenericType && type.GetGenericTypeDefinition()
                == typeof(List<>))
        {
            Type itemType = type.GetGenericArguments()[0]; // use this...

            ModelMetadata meta = ModelMetadataProviders.Current.GetMetadataForType(null, itemType);
            var filters = meta.Properties.Where(modelMetadata => !hideFilters.Contains(modelMetadata.PropertyName) && modelMetadata.AdditionalValues.Any(ad => ad.Key == "Filter"))
                .Select(metadata => new
                    {
                        PropertyName = metadata.AdditionalValues.ContainsKey("PropertyName") ? metadata.AdditionalValues["PropertyName"].ToString() : metadata.PropertyName,
                        Type = metadata.AdditionalValues["Filter"],
                        TableName = metadata.AdditionalValues["MasterTable"],
                        metadata.DisplayName
                    }).ToList();

            if (filters.Count>0)
            {%>
                <table>
               
            <%}
            
            foreach (var filter in filters)
            {
                if (filter.Type != null && filter.TableName != null && filter.Type.ToString().Trim().ToLower() == "foriegnfilter")
                {
                    List<KeyValuePair<string, string>> items = Smartec.Accounts.Camel.AMS.Model.Facades.AMSManagerFacade.GetListItemsByTableName(filter.TableName.ToString());
                    items.Insert(0, new KeyValuePair<string, string>("-1", "All"));
                    int maxLengthIndex = 0;
                    for (int i = 0; i < items.Count; i++)
                    {
                        if (items[maxLengthIndex].Value.Length < items[i].Value.Length)
                        {
                            maxLengthIndex = i;
                        }
                    }
                    
                %>
                 <tr><td>
                    <%:filter.DisplayName%>&nbsp;
                    </td>
                   <%-- <%: Html.DropDownList(filter.PropertyName, new SelectList(items, "Key", "Value", Model), new { onChange = "submit();" })%>--%>
                   
                   <td>
                   <%: Html.Telerik().ComboBox()
                                      .Name(filter.PropertyName)
                                      .AutoFill(true)
                                      .SelectedIndex(0)
                                      .BindTo(new SelectList(items, "Key", "Value", Model))
                                      //.HtmlAttributes(new { style = "width:692px;"})
                                      .HighlightFirstMatch(true)
                                      .ClientEvents(events => events.OnChange("CallSubmit"))
                                      .OpenOnFocus(true)
                                      %>
                   
                   &nbsp;&nbsp;&nbsp;&nbsp;

                   <div class="t-item" id="invisdiv<%:filter.PropertyName %>" style="background-color:Black;color:White;visibility:hidden;display:inline-block;">
                   <%:items[maxLengthIndex].Value %>
                   </div>

                   <script type="text/javascript">

                       $(document).ready(function () {
                           var comboWidth<%:filter.PropertyName %> = $("#invisdiv<%:filter.PropertyName %>").width() +40;
                           $("#<%:filter.PropertyName %>").prev().parent().width(comboWidth<%:filter.PropertyName %>);
                       });
    
                   </script>
                   </td></tr>
                <% 
                }
            }
            
            if (filters.Count>0)
            {%>
               
                </table>
            <%}
        }
%>


 <input id="invisSubmit" type="submit" value="Edit" style="display:none;" />
<script type="text/javascript">
    function CallSubmit() { $("#invisSubmit").click(); }
   
</script>